package com.zijing2333.adaptivescheduling.SwitcherManager;

import android.os.RemoteException;
import android.util.Log;

import com.zijing2333.adaptivescheduling.ChannelManager;
import com.zijing2333.adaptivescheduling.criticalNodeJudgment.JudgeKeyNode;
import com.zijing2333.servicerunner.JSStatus;
import com.zijing2333.servicerunner.TaskInfo;

import java.util.Map;

/**
 * 任务执行管理类
 */
public class TaskExecuteManager {
    private static final String TAG = "fun_TaskExecuteManager";

    private TaskRunnerConnectionManager taskRunnerConnectionManager;
//    private TaskStateStorageManager taskStateStorageManager;

    /**
     * 构造方法
     *
     * @param taskRunnerConnectionManager 任务运行连接管理器
     */
    public TaskExecuteManager(TaskRunnerConnectionManager taskRunnerConnectionManager) {
        this.taskRunnerConnectionManager = taskRunnerConnectionManager;
//        this.taskStateStorageManager = taskStateStorageManager;
    }

    /**
     * 部署JavaScript任务
     *
     * @param taskId   任务ID
     * @param switchTaskInfo 任务信息
     */
    public boolean dispatchJsTask(int type,String taskId, SwitchTaskInfo switchTaskInfo) {
        try {
            if (taskRunnerConnectionManager.getSwitcherService() != null) {
                return taskRunnerConnectionManager.getSwitcherService().deployTask(type,taskId,switchTaskInfo.getUrl(),switchTaskInfo.getIsFrontendRequired());
            } else {
                Log.e(TAG, "Service not connected, failed to get current execution state1");
                return false;
            }
        } catch (RemoteException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取当前执行状态
     *
     * @param serviceId 服务ID
     * @return 当前执行状态
     */
    public String getCurrentExecutionState(String serviceId) {
        try {
            if (taskRunnerConnectionManager.getSwitcherService() != null) {
                String executionState = taskRunnerConnectionManager.getSwitcherService().getCurrentExecutionState(serviceId);
//                taskStateStorageManager.setExecutionState(serviceId, executionState);
//                ChannelManager.getInstance().setExecutionState(serviceId,executionState);
                return executionState;
            } else {
                Log.e(TAG, "Service not connected, failed to get current execution state2");
                return null;
            }
        } catch (RemoteException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 恢复服务数据状态
     *
     * @param serviceId 服务ID
     */
    public void restoreServiceDataState(String serviceId,String data) {
        try {
            if (taskRunnerConnectionManager.getSwitcherService() != null) {
                if (data != null) {
                    taskRunnerConnectionManager.getSwitcherService().restoreServiceDataState(serviceId, data);
                } else {
                    Log.e(TAG, "Failed to restore service data state: execution state is null");
                }
            } else {
                Log.e(TAG, "Service not connected, failed to restore service data state");
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止服务执行
     *
     * @param serviceId 服务ID
     */
    public void stopJSExecution(String serviceId) {
        try {
            if (taskRunnerConnectionManager.getSwitcherService() != null) {
                taskRunnerConnectionManager.getSwitcherService().stopJSExecution(serviceId);
            } else {
                Log.e(TAG, "Service not connected, failed to stop JavaScript execution1");
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    public void deleteReplicaCode(String serviceId) {
        try {
            if (taskRunnerConnectionManager.getSwitcherService() != null) {
                taskRunnerConnectionManager.getSwitcherService().deleteReplicaCode(serviceId);
            } else {
                Log.e(TAG, "Service not connected, failed to delete replica code");
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    public Map<String, JSStatus> getJSServiceStatusMap(){
        try {
            if (taskRunnerConnectionManager.getSwitcherService() != null) {
                return taskRunnerConnectionManager.getSwitcherService().getServiceStatuses();
            } else {
                Log.e(TAG, "Service not connected, failed to get JSServiceStatusMap");
                return null;
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

}